/*
 * Copyright 2009 Cedric Priscal
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */

package android_serialport_api.sample;

import java.io.IOException;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.clouddot.baipai.R;

public class ConsoleActivity extends SerialPortActivity {

    EditText mReception;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_t_console);

//		setTitle("Loopback test");
        mReception = (EditText) findViewById(R.id.EditTextReception);

        EditText Emission = (EditText) findViewById(R.id.EditTextEmission);
        Emission.setOnEditorActionListener(new OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                int i;
                CharSequence t = v.getText();
                char[] text = new char[t.length()];
                for (i = 0; i < t.length(); i++) {
                    text[i] = t.charAt(i);
                }
                try {
                    mOutputStream.write(new String(text).getBytes());
                    mOutputStream.write('\n');
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return false;
            }
        });
    }

    private boolean isNew = true;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isNew = true;
        }
    };

    @Override
    protected void onDataReceived(final byte[] buffer, final int size) {
        runOnUiThread(new Runnable() {
            public void run() {
                if (mReception != null) {
                    if (isNew) {
                        isNew = false;
                        mReception.setText("");
                    }
                    mReception.append(new String(buffer, 0, size));
                    handler.removeMessages(1);
                    handler.sendEmptyMessageDelayed(1, 1000);

                    try {
                        String str = "";
                        str = new String(buffer, 0, size, "utf-8");
                        Log.e("a_t_console", "utf-8 ondatareceived:" + str + "," + size);
                        str = new String(buffer, 0, size, "GB2312");
                        Log.e("a_t_console", "gb2312 ondatareceived:" + str + "," + size);
                        str = new String(buffer, 0, size, "ASCII");
                        Log.e("a_t_console", "ASCII ondatareceived:" + str + "," + size);
                        str = new String(buffer, 0, size, "ISO-8859-1");
                        Log.e("a_t_console", "ISO-8859-1 ondatareceived:" + str + "," + size);
                        str = new String(buffer, 0, size, "GBK");
                        Log.e("a_t_console", "GBK ondatareceived:" + str + "," + size);
                        str = new String(buffer, 0, size, "UTF-16");
                        Log.e("a_t_console", "UTF-16 ondatareceived:" + str + "," + size);
                    } catch (Throwable e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
}
